﻿package
{
	import flash.events.Event;
	import flash.utils.Dictionary;
	import flash.display.*;
		
	import com.urbansquall.litmus.*;
	
	public class BuiltInBitmapParticleDemo extends AbstractTest
	{
		protected var m_particles:Dictionary;
		protected var m_speeds:Dictionary;
		protected var m_data : BitmapData;
		protected var m_counter : int;
		
		public function BuiltInBitmapParticleDemo():void
		{
			m_particles = new Dictionary(true);
			m_speeds = new Dictionary();
			m_data = new Main.ParticlePNG().bitmapData;
			
			this.addEventListener( Event.ENTER_FRAME, enterFrameHandler );
			this.mouseEnabled = false;
			this.mouseChildren = false;
			
			m_counter = 0;
		}
		
		override public function get title() : String { return "Built in Bitmap Renderer Particle Test"; }
		
		override public function dispose() : void
		{
			super.dispose();
			
			this.removeEventListener( Event.ENTER_FRAME, enterFrameHandler );
			m_particles = null;
			m_speeds = null;
			m_data = null;
		}
		
		private function createParticle() : void
		{
			var particle:Bitmap= new Bitmap( m_data );
			particle.x = 0;
			particle.y = m_counter % this.stage.stageHeight;
			this.addChild( particle );
			m_particles[ particle ] = particle;
			m_speeds[ particle ] = ((m_counter++) % 5) + 1;
		}
		
		private function enterFrameHandler(e:Event):void
		{
			for( var i : int = 0; i < Main.PARTICLES_PER_FRAME; i++ )
			{
				createParticle();
			}

			var particle : Bitmap;
			for each( particle in m_particles )
			{
				particle.x += m_speeds[ particle ];
				if ( particle.x > this.stage.stageWidth)
				{
					destroyParticle( particle );
				}
			}
		}
		
		private function destroyParticle( a_particle : Bitmap ) : void
		{
			this.removeChild( a_particle );
			delete m_particles[ a_particle ];
			delete m_speeds[ a_particle ];
		}
		
		
	}
}